# 实现一个函数用于判断字符串 str2 是否是 str1 的子串
# 如果是，则该函数返回 str2 在 str1 中首次出现的地址
# 否则，返回 None

str1 = 'asdfg'
str2 = 'aasdfghjklasdfgh'
str3 = 'aaaaasdfgqqwerxasd'

def find_substr(str1, str2):
    length1 = len(str1)
    length2 = len(str2)

    if length2 > length1:
        str1, str2 = str2, str1
        lentgth1, length2 = length2, length1

# string: asdfg fg
# i in range(0,5-2+1):
# 'asdfg'[i:i+2]
# 0 0:2 as
# 1 1:3 sd
# 2 2:4 df
# 3 3:5 fg

    for i in range(length1 - length2 + 1):
        if str2 == str1[i:i+length2]:
            print("String '{}' is the substr of string '{}'.".format(str2, str1))
            print("The first position of String '{}' is {}-{}.".format(str1, i, i + length2))
    else:
        print('None')

print("The Computational Results:")
find_substr(str1, str2)
print('=' * 65)
find_substr(str1, str3)
print('=' * 65)
find_substr(str2, str3)

# 两种思路都是对的，非常好。

# The Computational Results:
# String 'asdfg' is the substr of string 'aasdfghjklasdfgh'.
# The first position of String 'aasdfghjklasdfgh' is 1-6.
# =================================================================
# String 'asdfg' is the substr of string 'aaaaasdfgqqwerxasd'.
# The first position of String 'aaaaasdfgqqwerxasd' is 4-9.
# =================================================================
# None
